package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.UrlUtility;

import model.UserDTO;
import model.search.UserSearchDTO;
import bizlogic.UserManager;

@WebServlet("/LoadUser")
public class LoadUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LoadUserServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doShow(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doShow(request, response);
	}

	private void doShow(HttpServletRequest req, HttpServletResponse res) {
		UserManager um = new UserManager();
		
		String type = req.getParameter("role");
		if ( type == null ) type = "all";
		
		UserSearchDTO src = new UserSearchDTO();
		if ( !type.trim().equalsIgnoreCase("all") )
			src.setType(type);
		
		ArrayList<UserDTO> list = um.findUserByCriteria(src);
		req.setAttribute("role", type);
		req.setAttribute("list", list);
		
		UrlUtility.forwardPage(req, res, "/WEB-INF/ShowUser.jsp");
	}

}
